package MyThread_demo.shendu_study_ReentrantLock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ChenYongHao
 * @date 2023/5/4 14:24
 * @desc
 */
public class 锁超时 {
    public static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            System.out.println("t1启动...");
            if (!lock.tryLock()) {
                System.out.println("t1获取失败，返回");
                return;
            }
            try {
                System.out.println("t1获得了锁");
            } finally {
                lock.unlock();
            }
        }, "t1");
        lock.lock();
        System.out.println("main获得了锁");
        t1.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
